package com.zq.aop;

import com.zq.verify.Verify;
import org.springframework.aop.Pointcut;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;

/**
 * 自定义AOP
 */
public class VerifyPointCutAdvisorBulider {

    public VerifyPointCutAdvisorBulider() {
    }

    public VerifyPointCutAdvisorBulider(Verify verify, String controllerPath) {
        this.verify = verify;
        this.controllerPath = controllerPath;
    }

    private Verify verify;
    private String controllerPath;

    public Verify getVerify() {
        return verify;
    }

    public String getControllerPath() {
        return controllerPath;
    }

    public void setVerify(Verify verify) {
        this.verify = verify;
    }

    public void setControllerPath(String controllerPath) {
        this.controllerPath = controllerPath;
    }

    private Pointcut createPointCut(String controllerPath) {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("execution(public * "+controllerPath+"..*(..))");
        return pointcut;
    }

    private VerifyBeforeAdvice createAdvice () {
        VerifyBeforeAdvice beforeAdvice = new VerifyBeforeAdvice();
        beforeAdvice.setVerify(verify);
        return beforeAdvice;
    }

    public DefaultPointcutAdvisor bulid () {
        DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor();

        Pointcut pointCut = createPointCut(controllerPath);
        defaultPointcutAdvisor.setPointcut(pointCut);

        VerifyBeforeAdvice beforeAdvice = createAdvice();
        defaultPointcutAdvisor.setAdvice(beforeAdvice);
        return defaultPointcutAdvisor;
    }
}
